// pages/Manager/ManagerHome/ManagerHome.js
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetStoreManagerConfig , queryPetServiceOrdrStatistics } from '../../../api/merchant';
import { getHashParam } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    safaBtm: wx.getSystemInfoSync().screenHeight - wx.getSystemInfoSync().safeArea.bottom,
    CustomBar: app.globalData.CustomBar,
    todayOrderAmount: 0, // 今日成交额
    todayOrderNumber: 0, // 今日订单数量
    sumOrderAmount: 0, // 累计成交额
    sumOrderNumber: 0, // 累计订单数量
    configId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._queryPetStoreManagerConfig();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this._queryPetStoreManagerConfig();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 扫一扫
  scanCode(){
    let _this = this;
    wx.scanCode({
      success(res){
        console.log(res,'res')
        var userId = getHashParam(res.result,'userId');
        if(userId){
          wx.navigateTo({
            url: `/pages/Manager/UserOrder/UserOrder?userId=${userId}&storeId=${_this.data.store.storeId}`,
          })
        }
      },
    });
    
  },
  // 页面跳转
  goNext(e){
    let _this = this;
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}&storeId=${JSON.stringify(_this.data.store.storeId)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?storeId=${JSON.stringify(_this.data.store.storeId)}`,
          events: {
            'selStore': function(data){
              console.log(data,'data');
              _this.setData({
                configId: data.data
              })
              _this._queryPetStoreManagerConfig();
            }
          }
        })
      }
    }
  },
  // 获取门店管理员配置
  _queryPetStoreManagerConfig(){
    let _this = this;
    queryPetStoreManagerConfig({id: _this.data.configId})
    .then(res => {
      if(res.success){
        _this.setData({
          store: res.data.content.length > 0 ? res.data.content[0] : {},
        })
        if(res.data.content.length > 0){
          _this._queryPetServiceOrdrStatistics();
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查询服务订单统计
  _queryPetServiceOrdrStatistics(){
    let _this = this;
    queryPetServiceOrdrStatistics({storeId: _this.data.store.storeId})
    .then(res => {
      if(res.success){
        _this.setData({
          todayOrderAmount: res.data.todayOrderAmount,
          sumOrderAmount: res.data.sumOrderAmount,
          todayOrderNumber: res.data.todayOrderNumber,
          sumOrderNumber: res.data.sumOrderNumber,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 手机查询
  searchPhone(){
    let _this = this;
    wx.navigateTo({
      url: `/pages/Manager/SearchUserByPhone/SearchUserByPhone?&storeId=${JSON.stringify(_this.data.store.storeId)}`,
    })
  },
})